#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using ZO.SmartCore.Core;
using ZO.SmartCore.Plugins;
using ZO.SmartCore.Protocols.XMPP.Core;
using ZO.SmartCore.Windows.Forms.OptionControls;
using ZO.SmartMessenger.Plugin.GoogleTalk.Properties;
using ZO.SmartMessenger.Plugins;
using Version= ZO.SmartCore.Core.Version;

#endregion

namespace ZO.SmartMessenger.Plugin.GoogleTalk
{
    /// <summary>
    /// Provides Google Talk Protcol Support
    /// </summary>
    [Plugin]
    public class GTalkProtocolPlugin : ProtocolPlugin
    {


        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="GTalkProtocolPlugin"/> class.
        /// </summary>
        public GTalkProtocolPlugin()
        {
            base.Server = GMAIL_SERVER;
            base.Port = GMAIL_SERVER_PORT;
            jabberClient.Address.Host = GMAIL_SERVER;
            jabberClient.Address.Domain = GMAIL_DOMAIN;
            jabberClient.Address.Port = GMAIL_SERVER_PORT;

            jabberClient.ConnectCompleted += new EventHandler(jabberClient_ConnectCompleted);
            jabberClient.Connecting += new EventHandler(jabberClient_Connecting);
            jabberClient.Error += new EventHandler<ExceptionEventArgs>(jabberClient_Error);
        }

    
     

        #endregion

        #region Destructor
        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="T:SmartMessenger.Plugin.GoogleTalk.GTalkProtocolPlugin"/> is reclaimed by garbage collection.
        /// </summary>
        ~GTalkProtocolPlugin()
        {
            jabberClient.ConnectCompleted -= new EventHandler(jabberClient_ConnectCompleted);

        }
        #endregion

        #region Fields
        private XmppClient jabberClient = new XmppClient();


/*
		private GTalKOption _OptionWindow = new GTalKOption();
*/

        private const string GMAIL_SERVER = "talk.google.com";
        private const string GMAIL_DOMAIN = "google.com";

        private const int GMAIL_SERVER_PORT = 5222;
        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties


        /// <summary>
        /// Gets a value indicating whether this plugin is connected to server.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this plugin is connected; otherwise, <c>false</c>.
        /// </value>
        public override bool Connected
        {
            get { return jabberClient.Connected; }
        }

		/// <summary>
		/// Gets the option window used to set the option for plugin.
		/// </summary>
		/// <value>The option window.</value>
		public override SmartOptionPage OptionWindow
		{
			get
			{
				return new GTalKOption();
			}
		}


        /// <summary>
        /// Gets the name of the protocol.
        /// </summary>
        /// <value>The name of the protocol.</value>
        public override string ProtocolName
        {
            get { return "Google Talk"; }
        }

        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        /// <value>The username.</value>
        public override string Username
        {
            get
            {
                return base.Username;
            }
            set
            {
                base.Username = value;
                jabberClient.Username = value;
            }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public override string Password
        {
            get
            {
                return base.Password;
            }
            set
            {
                base.Password = value;
                jabberClient.Password = value;
            }
        }

        /// <summary>
        /// Gets or sets the server.
        /// </summary>
        /// <value>The server.</value>
        public override string Server
        {
            get
            {
                return base.Server;
            }
            set
            {
                base.Server = value;
                jabberClient.Address.Host = value;
            }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Override This Method To Dispose Unmanaged Resources.
        /// </summary>
        protected override void DisposeManagedResources()
        {
            jabberClient.ConnectCompleted -= new EventHandler(jabberClient_ConnectCompleted);
            jabberClient.Connecting -= new EventHandler(jabberClient_Connecting);
            jabberClient.Error -= new EventHandler<ExceptionEventArgs>(jabberClient_Error);

            if (jabberClient.Connected)
            {
                jabberClient.Disconnect();
            }
            base.DisposeManagedResources();
        }

        /// <summary>
        /// Handles the Error event of the jabberClient control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:SmartCore.Core.ExceptionEventArgs"/> instance containing the event data.</param>
        private void jabberClient_Error(object sender, ExceptionEventArgs e)
        {
            base.OnError(e);
        }

        /// <summary>
        /// Handles the ConnectCompleted event of the jabberClient control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void jabberClient_ConnectCompleted(object sender, EventArgs e)
        {
            Console.WriteLine("Connected to Google");
            base.OnConnectCompleted(e);
        }


        /// <summary>
        /// Handles the Connecting event of the jabberClient control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void jabberClient_Connecting(object sender, EventArgs e)
        {
            Console.WriteLine("Connecting to Google");
            OnConnecting(e);
        }
  
        #endregion



        #region IPluginInfo Members

        /// <summary>
        /// Returns the name of this plugin.
        /// </summary>
        /// <value>Plugin Name</value>
        public override string Name
        {
            get { return "Google Talk Protocol"; }
        }

        /// <summary>
        /// Returns the ID of this plugin.
        /// </summary>
        /// <value>plugin Id must be unique</value>
        public override Guid ID
        {
            get { return new Guid("{296DAE77-0AA2-4741-AAEC-7CA2186F12A6}"); }
        }



        /// <summary>
        /// Gets the image that will be displayed in the About dialog.
        /// </summary>
        /// <value>plugin logo if any</value>
        public override PluginImage Image
        {
            get
            {
                return new PluginImage(Resources.GTI_Red_akkasone);
            }
        }


        /// <summary>
        /// this info is used if Application want to show info about updating assembly
        /// </summary>
        /// <value>URL Update Text</value>
        public override string UpdateText
        {
            get { return String.Empty; }
        }

        /// <summary>
        /// Gets the title of the plugin module displayed in the About dialog.
        /// </summary>
        /// <value>plugin title</value>
        public override string Title
        {
            get { return "Google Talk Protocol 1.0"; }
        }

        /// <summary>
        /// A short description of this plugin.
        /// </summary>
        /// <value>plugin description</value>
        public override string Description
        {
            get { return "provide Google Talk instant messaging"; }
        }



        /// <summary>
        /// Gets the plugin trademark.
        /// </summary>
        /// <value>The trademark.</value>
        public override string Trademark
        {
            get { return "Copyright (c) 2006 Zero Override, All right reserved."; }
        }

        /// <summary>
        /// Gets the plugin copyright.
        /// </summary>
        /// <value>The copyright.</value>
        public override string Copyright
        {
            get { return "Copyright (c) 2006 Zero Override."; }
        }

  
        /// <summary>
        /// Get the version of this product.
        /// </summary>
        /// <value>Plugin Version</value>
        public override Version Version
        {
            get { return new Version(1, 0, ReleaseType.Beta, 0); }
        }


        #endregion

        #region ProtocolPlugin Members

        /// <summary>
        /// Connects this instance.
        /// </summary>
        public override void Connect()
        {
            if (!Connected)
            {
                jabberClient.Connect();
            }

        }

        /// <summary>
        /// Disconnects this instance.
        /// </summary>
        public override void Disconnect()
        {
            if (Connected)
            {
                jabberClient.Disconnect();
            }

        }

        /// <summary>
        /// Connects to server using this protocol in async mode.
        /// </summary>
        public override void ConnectAsync()
        {
            if (!Connected)
            {
                jabberClient.ConnectAsync();
            }

        }
        #endregion







    }
}
